package com.yanqu.road.server.cmd.activity.rankunionwar.cross;

import com.yanqu.road.entity.activity.rankunionwar.config.RankUnionWarConfig;
import com.yanqu.road.entity.enums.eLogMoneyType;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.RankUnionWarProto;
import com.yanqu.road.server.cmd.base.AbstractCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.gameplayer.GamePlayer;
import com.yanqu.road.server.gameplayer.module.player.CurrencyModule;
import com.yanqu.road.server.gameplayer.module.player.PreReduceModule;
import com.yanqu.road.server.manger.activity.ActivityMgr;
import com.yanqu.road.server.manger.activity.rankunionwar.RankUnionWarMgr;
import com.yanqu.road.server.protocol.ClientProtocol;
import com.yanqu.road.server.protocol.GameErrorCode;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.utils.property.PropertyHelper;

@Cmd(code = GameProtocol.S_RANKUNIONWAR_INSPIRE_RES, desc = "")
public class RankUnionWarInspireResCmd extends AbstractCommand {

    int ret;
    String cost;
    String reward = "";

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        RankUnionWarProto.CrossRankUnionWarInspireRespMsg reqMsg = RankUnionWarProto.CrossRankUnionWarInspireRespMsg.parseFrom(packet.getBytes());
        ret = reqMsg.getRet();
        cost = reqMsg.getCost();
        return true;
    }

    @Override
    public void execute(GamePlayer player, YanQuMessage packet) throws Exception {
        RankUnionWarConfig config = RankUnionWarMgr.getConfig();
        if (config == null || !ActivityMgr.activityInShowTime(config.getActivityInfo())) {
            ret = GameErrorCode.E_ACTIVITY_NO_FOUND;
            return;
        }
        if(!"".equals(cost)){
            String costStr = cost;
            player.getModule(PreReduceModule.class).restorePreReduce(costStr);
            if(ret == 0){
                player.getModule(CurrencyModule.class).removeCurrency(PropertyHelper.parseStringToProperty(costStr), eLogMoneyType.RankUnionWar,eLogMoneyType.RankUnionWarInspireReward);
            }
        }
        if(ret == 0){
            reward = RankUnionWarMgr.randomInspireReward();
            if(!"".equals(reward)){
                player.getModule(CurrencyModule.class).addCurrency(reward,eLogMoneyType.RankUnionWar,eLogMoneyType.RankUnionWarInspireReward);
            }
        }
    }

    @Override
    public void sendData(GamePlayer player) {
        RankUnionWarProto.RankUnionWarInspireRespMsg.Builder respMsg = RankUnionWarProto.RankUnionWarInspireRespMsg.newBuilder();
        respMsg.setRet(ret);
        respMsg.setReward(reward);
        player.sendPacket(ClientProtocol.U_RANKUNIONWAR_INSPIRE, respMsg);
    }
}
